//
//  MainViewController.m
//  UITableView
//
//  Created by cocoazxj@163.com on 15/3/26.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "MainViewController.h"
#import "CustomCell.h"
#import "GroupModel.h"
#import "HeadView.h"
#import "FootView.h"

@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource,FootViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tv_main;
@property (nonatomic, strong) NSMutableArray *mArray;
@property (nonatomic, strong) NSTimer *mTimer;
@property (nonatomic, strong) HeadView *mV;
@property (nonatomic, assign) NSInteger page;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setAd];
    FootView *v = [[[NSBundle mainBundle] loadNibNamed:@"FootView" owner:self options:nil] firstObject];
    v.delegate = self;
    self.tv_main.tableFooterView = v;
}

- (void)setAd
{
    // 广告数据源
    NSMutableArray *mArray = [NSMutableArray array];
    for(int i = 0; i < 5; i++)
    {
        [mArray  addObject:[NSString stringWithFormat:@"ad_%02d",i]];
        NSLog(@"----------%@",[NSString stringWithFormat:@"ad_%02d",i]);
    }
    
    HeadView *v = [[HeadView alloc] initWithHeadView:mArray];
    
//    self.mTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//    [[NSRunLoop mainRunLoop] addTimer:self.mTimer forMode:NSDefaultRunLoopMode];
    self.tv_main.tableHeaderView = v;
}

- (void)nextPage
{
    self.page = self.mV.pc_num.currentPage;
    self.page ++;
    if (self.page == 5)
    {
        self.page = 0;
    }
    [self.mV.sv_main setContentOffset:CGPointMake(250*self.page, 0) animated:YES];
}

- (void)loadingMore
{
    GroupModel *m = [[GroupModel alloc] init];
    m.name = @"Hello Delegate";
    [self.mArray addObject:m];
    [self.tv_main reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)mArray
{
    if(!_mArray)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs" ofType:@".plist"];
        NSArray *a = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *ma = [NSMutableArray array];
        for (NSDictionary *d in a)
        {
            [ma addObject:[GroupModel groupWithDict:d]];
        }
        _mArray = ma;
    }
    return _mArray;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [CustomCell customCell:tableView];
    cell.groupModel = [_mArray objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end
